//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package me.work.pay.jsyl.app.utils;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

import com.chaomeng.a8sdklibrary.utils.AppUtils;

import org.greenrobot.eventbus.EventBus;

public class SBLEManager {
    public BluetoothAdapter mBluetoothAdapter;
    private static volatile SBLEManager defaultInstance;
    private Context context;
    private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("android.bluetooth.adapter.action.STATE_CHANGED".equals(action)) {
                switch (SBLEManager.this.mBluetoothAdapter.getState()) {
                    case 12:
                        String bleName = SBLEManager.this.mBluetoothAdapter.getName();
                        String sn = AppUtils.getSerialNumber();
                        sn = sn.substring(sn.length() - 4);
                        if (bleName == null || bleName.isEmpty()) {
                            SBLEManager.this.mBluetoothAdapter.setName("CM-A8-" + sn);
                        }

                        SBLEManager.this.startTheService();
                        Toast.makeText(context, "等待联网中...", Toast.LENGTH_SHORT).show();
                }
            }

        }
    };

    public SBLEManager() {
    }

    public static SBLEManager getDefault() {
        if (defaultInstance == null) {
            Class var0 = SBLEManager.class;
            synchronized (SBLEManager.class) {
                if (defaultInstance == null) {
                    defaultInstance = new SBLEManager();
                }
            }
        }

        return defaultInstance;
    }

    public void initBlueTooth(Context context) {
        this.context = context;
        this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (this.mBluetoothAdapter == null) {
            Toast.makeText(context, "蓝牙功能不可用", Toast.LENGTH_SHORT).show();
        } else {
            this.startTheService();
        }
    }

    Intent service;

    private void startTheService() {
        if (!this.mBluetoothAdapter.isEnabled()) {
            this.mBluetoothAdapter.enable();
        }
        service = new Intent(this.context, SBleService.class);
        this.context.startService(service);
    }

    public void unregisterReceiver() {

    }

    public void registerReceiver() {
        if (!EventBus.getDefault().isRegistered(this.context)) {
            EventBus.getDefault().register(this.context);
        }

        IntentFilter filter = new IntentFilter();
        filter.addAction("android.bluetooth.adapter.action.STATE_CHANGED");
        this.context.registerReceiver(this.mBluetoothReceiver, filter);
    }
}
